-- UIBineSkillLearn
-- Created by chengb Apr/18/2015
-- 魔藤技能学习

UIBineSkillLearn = class("UIBineSkillLearn", function()
    return cc.Layer:create();
end);

function UIBineSkillLearn.create(skillId)
    return UIBineSkillLearn.new(skillId);
end

function UIBineSkillLearn:ctor(skillId)
    -- 初始化
    self:setName("UIBineSkillLearn");
    local node = cc.CSLoader:createNode("layout/bine/BineSkillLearn.csb");
    self:addChild(node);
    self.node = node;

    self:setLocalZOrder(UIMgr.TOP_MENU_ZORDER + 1);

    self.skillId = skillId;

    -- 注册事件处理回调函数
    self:registerEventCallback()

    -- 注册点击事件
    self:registerTouchEvent();

    -- 重绘
    self:redraw();

    -- 适配
    self:resize();
end

-- 适配
function UIBineSkillLearn:resize()
    local node = self.node;

    -- bg居中显示并充满整个屏幕
    AlignM.fitToScreen(node, "bg");

    -- CT居中并缩放至合适大小
    AlignM.alignToCenter(node, "CT");

    -- BT置于底部并缩放至合适大小
    AlignM.alignToBottomCenter(node, "BT");
end

-- 注册事件处理回调函数
function UIBineSkillLearn:registerEventCallback()
    -- 关注魔藤学习技能事件
    EventMgr.register("UIBineSkillLearn", event.BINE_LEARN_SKILL, function(args)
        -- 关闭界面
        UIMgr.getCurrentScene():removeFormByName("UIBineSkillLearn");
    end);

    self:registerScriptHandler(function(eventType)
        if eventType == "exit" then
            cclog("UIBineSkillLearn 界面析构清理");

            -- 移除所有事件关注
            EventMgr.removeAll("UIBineSkillLearn");
        end
    end);
end

-- 注册点击事件
function UIBineSkillLearn:registerTouchEvent()
    -- 注册返回按钮点击事件
    local btnBack = findChildByName(self.node, "BT/btn_back");
    local function onBackClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            AudioM.playFx("button_return");
            -- 关闭当前界面
            UIMgr.getCurrentScene():removeFormByName("UIBineSkillLearn");
        end
    end
    AddBackEventHandler(self, btnBack, onBackClick);
    TextStyleM.setTextStyle(btnBack, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnBack:setTitleText(getLocStr("btn_text_back"));

    -- 注册确定按钮点击事件
    local btnOk = findChildByName(self.node, "CT/btn_ok");
    local function onOkClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            AudioM.playFx("button_select");
            -- 尝试学习
            self:tryLearnSkill();
        end
    end
    btnOk:addTouchEventListener(onOkClick);
    TextStyleM.setTextStyle(btnOk, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnOk:setTitleText(getLocStr("btn_text_bine_learn"));
end

-- 重绘
function UIBineSkillLearn:redraw()
    -- 技能名称
    local nameLabel = findChildByName(self.node, "CT/name");
    local skillName = SkillM.query(self.skillId, "name");
    local descLabel = findChildByName(self.node, "CT/desc");
    TextStyleM.setTextStyle(nameLabel, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_DARK_RED);
    nameLabel:setString(skillName);

    local desc = "";

    -- 消耗的物品描述
    local cost = FormulaM.invoke("CALC_BINE_LEARN_SKILL_COST", self.skillId);
    local classId = table.keys(cost)[1];
    local itemDesc = ItemM.getItemDesc(classId);

    if itemDesc then
        desc = itemDesc .. "\n\n";
    end

    -- 学习提示
    desc = desc .. getLocStr("bine_skill_tip1") .. "\n";

    -- 施法方式
    local castType = SkillM.query(self.skillId, "type");
    if castType == USER_SKILL then
        -- 主动
        local mp = BineM.calcSkillCost(self.skillId);
        local castDesc = string.format(getLocStr("bine_skill_initiative_cast2"), mp);
        desc = desc .. castDesc .. "\n";
    end

    -- 技能描述（灰色显示）
    local skillDesc = SkillM.getSkillDesc(self.skillId);
    skillDesc = "[gray]" .. extractString(skillDesc) .. "[-]";
    desc = desc .. skillDesc;

    TextStyleM.setTextStyle(descLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_DARK_BROWN);
    descLabel = applyString(descLabel, desc);

    -- 重绘学习按钮
    -- 如果魔藤死亡了，按钮置灰
    local btnOk = findChildByName(self.node, "CT/btn_ok");
    if ME.user.bine:isDead() then
        btnOk:setTouchEnabled(false);
        btnOk:setBright(false);
    else
        btnOk:setTouchEnabled(true);
        btnOk:setBright(true);
    end

    -- 自适应所有组件
    local bg = findChildByName(self.node, "CT/bg");
    AlignM.adaptLayout(bg, AlignM.BG_SIZE_NORMAL, { nameLabel, descLabel, btnOk});
end

-- 尝试学习技能
function UIBineSkillLearn:tryLearnSkill()
    local ret = BineM.canLearnSkill(self.skillId);
    if ret ~= true then
        if ret == "cost_lack" then
            -- 消耗不足
            local cost = FormulaM.invoke("CALC_BINE_LEARN_SKILL_COST", self.skillId);
            local classId   = table.keys(cost)[1];
            local name      = ItemM.query(classId, "name");

            alert(string.format(getLocStr("item_not_enough"), name));
        end

        return;
    end

    -- 学习技能
    DungeonActionM.go("bine_learn_skill", 0, self.skillId);
end
